package com.zl.weixin.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zl.weixin.entity.AccessToken;
import com.zl.weixin.entity.Button;
import com.zl.weixin.entity.CommonButton;
import com.zl.weixin.entity.ComplexButton;
import com.zl.weixin.entity.Menu;
import com.zl.weixin.entity.UrlButton;
import com.zl.weixin.util.WeixinUtil;

/**
 * 菜单管理器类
 * 
 * @author liufeng
 * @date 2013-08-08
 */
public class MenuManager {
	private static Logger log = LoggerFactory.getLogger(MenuManager.class);

	public static void main(String[] args) {

		String appId = "wxac9a290c5dc49ed0";
		String appSecret ="24dd8e15cfeb82fc1c0ac60c8705d75c";
		// 调用接口获取access_token
		AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);

		if (null != at) {
			// 调用接口创建菜单
			int result = WeixinUtil.createMenu(getMenu(), at.getToken());

			// 判断菜单创建结果
			if (0 == result)
				log.info("菜单创建成功！");
			else
				log.info("菜单创建失败，错误码：" + result);
		}
	}

	/**
	 * 组装菜单数据
	 * 
	 * @return
	 */
	private static Menu getMenu() {
//		CommonButton btn11 = new CommonButton();
//		btn11.setName("我要举报");
//		btn11.setType("click");
//		btn11.setKey("11");
		UrlButton btn11 = new UrlButton();
		//CommonButton btn11 = new CommonButton();
		btn11.setName("平台首页");
//		btn11.setType("click");
//		btn11.setKey("11");
		btn11.setType("view");
//		btn11.setUrl("http://60.190.2.54:80/weixingongchong/page/report.jsp");
		btn11.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://121.43.226.252/zzd-web/index/index&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
		
		UrlButton btn12 = new UrlButton();
		btn12.setName("机构查询");
		btn12.setType("view");
		btn12.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://www.zhili-tech.com/zaozhidao-weixin/agency/index.html&response_type=code&scope=snsapi_base&state=1#wechat_redirect");

		UrlButton btn21 = new UrlButton();
		btn21.setName("信息查询");
		btn21.setType("view");
		btn21.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://121.43.226.252/zzd-web/search/index&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
		
		UrlButton btn22 = new UrlButton();
		btn22.setName("我的订阅");
		btn22.setType("view");
		btn22.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://121.43.226.252/zzd-web/subscribe/index&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
		
		UrlButton btn23 = new UrlButton();
		btn23.setName("订阅设置");
		btn23.setType("view");
		btn23.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://121.43.226.252/zzd-web/subscribe/rule&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
		
		
		UrlButton btn31 = new UrlButton();
		btn31.setName("个人中心");
		btn31.setType("view");
		btn31.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://121.43.226.252/zzd-web/center/index&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
	
//		CommonButton btn12 = new CommonButton();
//		btn12.setName("警情预报");
//		btn12.setType("click");
//		btn12.setKey("12");
		
		CommonButton btn13 = new CommonButton();
		btn13.setName("政府公告");
		btn13.setType("click");
		btn13.setKey("13");


//		CommonButton btn31 = new CommonButton();
//		btn31.setName("我的订阅");
//		btn31.setType("click");
//		btn31.setKey("31");

		UrlButton btn32 = new UrlButton();
		btn32.setName("意见反馈");
		btn32.setType("view");
		btn32.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxac9a290c5dc49ed0&redirect_uri=http://www.zhili-tech.com/zaozhidao-weixin/feedBack/index.html&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
	
		
		CommonButton btn33 = new CommonButton();
		btn33.setName("使用帮助");
		btn33.setType("click");
		btn33.setKey("33");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("信息查询");
		mainBtn1.setSub_button(new Button[] { btn11, btn12 });
		
		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("查询订阅");
		mainBtn2.setSub_button(new Button[] { btn21, btn22,btn32 });
		
		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("服务中心");
		mainBtn3.setSub_button(new Button[] { btn31, btn32,btn33 });

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		Menu menu = new Menu();
		menu.setButton(new Button[] { btn11, mainBtn2, btn31 });

		return menu;
	}
}
